#include "StdAfx.h"
#include "ZChinaArea.h"
#include "..\..\ZAllTools\ZDBBind\ZDbTableHelper.h"

ZImplementDynCreate(ZZone)
///
ZImplementDynCreate(ZCity)
void ZCity::LoadZoneFromDB( QSqlDatabase& db )
{
	QString sCityID;
	GetFieldVal("CitySort", sCityID);
	if (sCityID.length() > 0)
	{
		QString sCondition = "CityID=" + sCityID;

		ZDbTableHelper tableHelper(ZONE_TABLE_NAME, &db);
		m_vecZone = tableHelper.CreateItemByCondition<ZZone>(sCondition);
	}
}

vecStr ZCity::GetAllZoneNames()
{
	vecStr vecAllZoneNames;
	QString sCurProvinceName;
	for (auto it=m_vecZone.begin(); it!=m_vecZone.end(); it++)
	{
		if ((*it)->GetFieldVal("ZoneName", sCurProvinceName))
		{
			vecAllZoneNames.push_back(sCurProvinceName);
		}
	}

	return vecAllZoneNames;
}
///
ZImplementDynCreate(ZProvince)
void ZProvince::LoadCityFromDB( QSqlDatabase& db )
{
	QString sProvinceID;
	GetFieldVal("ProSort", sProvinceID);
	if (sProvinceID.length() > 0)
	{
		QString sCondition = "ProID=" + sProvinceID;

		ZDbTableHelper tableHelper(CITY_TABLE_NAME, &db);
		m_vecCity = tableHelper.CreateItemByCondition<ZCity>(sCondition);

		for (auto it=m_vecCity.begin(); it!=m_vecCity.end(); it++)
		{
			(*it)->LoadZoneFromDB(db);
		}
	}
}

vecStr ZProvince::GetAllCityNames()
{
	vecStr vecAllCityNames;
	QString sCurProvinceName;
	for (auto it=m_vecCity.begin(); it!=m_vecCity.end(); it++)
	{
		if ((*it)->GetFieldVal("CityName", sCurProvinceName))
		{
			vecAllCityNames.push_back(sCurProvinceName);
		}
	}

	return vecAllCityNames;
}

ZCity* ZProvince::GetCityByName( const QString& sCityName )
{
	QString sCurCityName;
	for (auto it=m_vecCity.begin(); it!=m_vecCity.end(); it++)
	{
		if ((*it)->GetFieldVal("CityName", sCurCityName))
		{
			if (sCurCityName == sCityName)
			{
				return *it;
			}
		}
	}

	return NULL;
}
//////////////////////////////////////////////////////////////////////////
void ZCountry::LoadFromDB( QSqlDatabase& db )
{
	ZDbTableHelper tableHelper(PROVINCE_TABLE_NAME, &db);
	m_vecProvince = tableHelper.CreateItemByCondition<ZProvince>("1=1");

	for (auto it=m_vecProvince.begin(); it!=m_vecProvince.end(); it++)
	{
		(*it)->LoadCityFromDB(db);
	}
}

ZCountry& ZCountry::instance()
{
	static ZCountry country;
	return country;
}

ZProvince* ZCountry::GetProvinceByName( const QString& sProvinceName )
{
	QString sCurProvinceName;
	for (auto it=m_vecProvince.begin(); it!=m_vecProvince.end(); it++)
	{
		if ((*it)->GetFieldVal("ProName", sCurProvinceName))
		{
			if (sCurProvinceName == sProvinceName)
			{
				return *it;
			}
		}
	}

	return NULL;
}

vecStr ZCountry::GetAllProvinceNames()
{
	vecStr vecAllProvinceNames;
	QString sCurProvinceName;
	for (auto it=m_vecProvince.begin(); it!=m_vecProvince.end(); it++)
	{
		if ((*it)->GetFieldVal("ProName", sCurProvinceName))
		{
			vecAllProvinceNames.push_back(sCurProvinceName);
		}
	}

	return vecAllProvinceNames;
}
